home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMIBEST2.ADF / Best of AMICUS 2 / C / PropGadget / prop.c < prev    next >
C/C++ Source or Header  |  1987-07-22  |  14KB  |  523 lines

  1. /*********************************************************
  2.   'C' program showing examples of Proportional Gadgets.         
  3.  
  4.   This was compiled using Manx, Aztec "C", AmigaDos V1.2      
  5.   The code depends on V1.2 functions to operate correctly.    
  6.   It is intended to be run from CLI.                
  7.                                  
  8.   Copyright (C) 1987 H. Maybeck Tolly, TollySoft        
  9.   This program is in the public domain and may be 
  10.   distributed free of charge.                                             
  11.                                                               
  12. **********************************************************/
  13.  
  14. #include <exec/types.h>
  15. #include <exec/exec.h>
  16. #include <intuition/intuition.h>
  17. #include <graphics/gfxbase.h>
  18. #include <functions.h>
  19.  
  20. #define visible_lines 5L
  21. #define total_lines 10L
  22. #define MAXLEN 20
  23.  
  24. struct IntuitionBase *IntuitionBase = 0L;
  25. struct GfxBase *GfxBase = 0L;
  26. struct Window *ControlWindow = 0L;
  27. struct IntuiMessage *MyIntuiMessage;
  28.  
  29.  
  30. /***********************************/
  31. /* Declare Prop Gadget structures. */
  32. /***********************************/
  33.  
  34. /* This Prop Gadget shows how to read Pot values to */ 
  35. /* scroll through a list of strings. */
  36.  
  37. struct Image g_image_scroll;
  38. struct PropInfo g_prop_scroll;
  39.  
  40. struct Gadget PropGadget_scroll = {
  41.  
  42.    NULL,             /* pointer to Next Gadget */
  43.    180, 30, 20, 60,  /* (Left Top Width Height) Hit Box */
  44.    GADGHCOMP ,       /* Flags */
  45.              /* Activation flags */
  46.    GADGIMMEDIATE | FOLLOWMOUSE | RELVERIFY,        
  47.    PROPGADGET ,      /* Type */
  48.    (APTR)&g_image_scroll,   /* pointer to Image */
  49.    NULL,             /* no pointer to SelectRender */
  50.    NULL,             /* pointer to GadgetText */
  51.    0,                /* MutualExclude  not implemented*/
  52.    (APTR)&g_prop_scroll,    /* pointer to SpecialInfo */  
  53.    0,                /* no ID */
  54.    NULL              /* no pointer to special data */
  55. };
  56.  
  57. /* This Prop Gadget points to a disabled String Gadget,  */
  58. /* to show a "Prop Fuzzy".                 */
  59.  
  60. /* This String Gadget is used only to show a property    */
  61. /* of Prop Gadgets. It is not intended as a clear example*/
  62. /* of a string gadget. Please refer to previous articles */
  63. /* in this series for more info on String Gadgets.       */   
  64.  
  65. struct StringInfo StrInfo = {
  66.     (UBYTE *)"static test string ",
  67.     NULL, 0, 20, 0,0,0,0,0,0,NULL,0,NULL};
  68.  
  69. struct Gadget StrGadget = {
  70.  
  71.    &PropGadget_scroll,
  72.    220,30,160,7,   
  73.    GADGHCOMP | GADGDISABLED ,
  74.    GADGIMMEDIATE | RELVERIFY,
  75.    STRGADGET,
  76.    NULL, NULL, NULL, 0, (APTR)&StrInfo, 0, NULL   
  77. };
  78.  
  79. struct Image g_image_fuzzy;
  80. struct PropInfo g_prop_fuzzy;
  81.  
  82. struct Gadget PropGadget_fuzzy = {
  83.  
  84.    &StrGadget,           /* pointer to Next Gadget */
  85.    400, 30, 20, 60,  /* (Left Top Width Height) Hit Box */
  86.    GADGHCOMP ,           /* Flags */
  87.    GADGIMMEDIATE | RELVERIFY,       /* Activation flags */
  88.    PROPGADGET ,          /* Type */
  89.    (APTR)&g_image_fuzzy, /* pointer to Image */
  90.    NULL,                 /* no pointer to SelectRender */
  91.    NULL,                 /* pointer to GadgetText */
  92.    0,                    /* MutualExclude not implemented*/
  93.    (APTR)&g_prop_fuzzy,  /* pointer to SpecialInfo */  
  94.    0,                    /* no ID */
  95.    NULL                  /* no pointer to special data */
  96. };
  97.  
  98.  
  99. /********************************************************/
  100. /* All images  must reside in chip memory (lower 512K). */
  101. /* If your compiler/linker does not have an option to do*/
  102. /* this, you must allocate chip memory, and copy this   */
  103. /* data into that memory before assigning the pointer   */
  104. /* members of the Gadget structure.            */
  105. /********************************************************/
  106.  
  107. UWORD prime[] =
  108.    {
  109. /* plane 1 */
  110.    0x0200,
  111.    0x0700,
  112.    0x0700,
  113.    0x0F80,
  114.    0x0F80,
  115.    0x1FC0,
  116.    0x1FC0,
  117.    0x3FE0,
  118.    0x7FF0,
  119.    0x3FE0,
  120.    0x1FC0,
  121.    0x1FC0,
  122.    0x0F80,
  123.    0x0F80,
  124.    0x0700,
  125.    0x0700,
  126.    0x0200,
  127.  
  128. /* plane 2 */
  129.    0xFFFF,
  130.    0xFFFF,
  131.    0xFFFF,
  132.    0xFFFF,
  133.    0xFFFF,
  134.    0xFFFF,
  135.    0xFFFF,
  136.    0xFFFF,
  137.    0xFFFF,
  138.    0xFFFF,
  139.    0xFFFF,
  140.    0xFFFF,
  141.    0xFFFF,
  142.    0xFFFF,
  143.    0xFFFF,
  144.    0xFFFF,
  145.    0xFFFF
  146. };
  147.  
  148.  
  149. UWORD alt[] =
  150.    {
  151. /* plane 1 */
  152.    0x7038,
  153.    0x3870,
  154.    0x1CE0,
  155.    0x0FC0,
  156.    0x0FC0,
  157.    0x0FC0,
  158.    0x0FC0,
  159.    0x0FC0,
  160.    0x0FC0,
  161.    0x0FC0,
  162.    0x0FC0,
  163.    0x0FC0,
  164.    0x0FC0,
  165.    0x0FC0,
  166.    0x1CE0,
  167.    0x3870,
  168.    0x7038,
  169.  
  170.  
  171. /* plane 2 */
  172.    0xFFFF,
  173.    0xFFFF,
  174.    0xFFFF,
  175.    0xFFFF,
  176.    0xFFFF,
  177.    0xFFFF,
  178.    0xFFFF,
  179.    0xFFFF,
  180.    0xFFFF,
  181.    0xFFFF,
  182.    0xFFFF,
  183.    0xFFFF,
  184.    0xFFFF,
  185.    0xFFFF,
  186.    0xFFFF,
  187.    0xFFFF,
  188.    0xFFFF
  189. };
  190.  
  191. struct Image g_image_primary = {
  192.  1,0,13,17,2,
  193.  prime,
  194.  0x03, 0x0, NULL
  195. };
  196.  
  197. struct Image g_image_alternate = {
  198.  1,0,13,17,2,
  199.  alt,
  200.  0x03, 0x0, NULL
  201. };
  202.  
  203. struct PropInfo g_prop_image;
  204.  
  205. struct Gadget PropGadget_image = {
  206.  
  207.    &PropGadget_fuzzy,       /* pointer to Next Gadget */
  208.    500, 30, 21, 60,  /* (Left Top Width Height) Hit Box */
  209.    GADGIMAGE | GADGHIMAGE,  /* Flags */
  210.    GADGIMMEDIATE | RELVERIFY, /* Activation flags */
  211.    PROPGADGET ,             /* Type */
  212.    (APTR)&g_image_primary,  /* pointer to Image */
  213.    (APTR)&g_image_alternate,/* no pointer to SelectRender*/
  214.    NULL,                    /* pointer to GadgetText */
  215.    0,                       /* Mut-Exc not implemented */
  216.    (APTR)&g_prop_image,     /* pointer to SpecialInfo */  
  217.    0,                       /* no ID */
  218.    NULL                     /* no special data */
  219. }; 
  220.  
  221. struct Image g_image_free = {
  222.  1,0,13,17,2,
  223.  alt,
  224.  0x03, 0x0, NULL
  225. };
  226.  
  227. struct PropInfo g_prop_free;
  228.  
  229. struct Gadget PropGadget_free = {
  230.  
  231.    &PropGadget_image,     /* pointer to Next Gadget */
  232.    200, 120, 80, 35,  /* (Left Top Width Height) Hit Box */
  233.    GADGHNONE | GADGIMAGE, /* Flags */
  234.    GADGIMMEDIATE | RELVERIFY, /* Activation flags */
  235.    PROPGADGET ,           /* Type */
  236.    (APTR)&g_image_free,   /* pointer to Image */
  237.    NULL,                  /* no SelectRender */
  238.    NULL,                  /* pointer to GadgetText */
  239.    0,                     /* Mut-Excl not implemented */
  240.    (APTR)&g_prop_free,    /* pointer to SpecialInfo */  
  241.    0,                     /* no ID */
  242.    NULL                   /* no pointer to special data */
  243. };
  244.  
  245. /********************************************************/
  246. /* IntuiText structures for the list of items to scroll */
  247. /********************************************************/
  248.  
  249.    struct IntuiText IntuiStr[5] = {
  250.     {2,3,JAM2,5,30,NULL,NULL,NULL},
  251.     {2,3,JAM2,5,45,NULL,NULL,&IntuiStr[0]},
  252.     {2,3,JAM2,5,60,NULL,NULL,&IntuiStr[1]},
  253.     {2,3,JAM2,5,75,NULL,NULL,&IntuiStr[2]},
  254.     {2,3,JAM2,5,90,NULL,NULL,&IntuiStr[3]}
  255. };
  256.  
  257. /********************************************************/
  258. /* Text strings through which to scroll.         */
  259. /* In our example they will retain the same values    */
  260. /* throughout the program. In a real application they    */
  261. /* would most likely change. We won't pad them with     */
  262. /* blanks now, so that we can show how to do it on    */
  263. /* the fly.                        */
  264. /********************************************************/
  265. char text_strings[10][20] = {
  266.     {"first string"},
  267.     {"second string"},
  268.     {"third string"},
  269.     {"fourth string"},
  270.     {"fifth string"},
  271.     {"sixth string"},
  272.     {"seventh string"},
  273.     {"eighth string"},
  274.     {"ninth string"},
  275.     {"tenth string"}
  276. };
  277.  
  278. /********************************************************/
  279. /* Labels for different Prop Gadgets            */
  280. /* Harri Tolly is not the Bandito            */
  281. /********************************************************/
  282.  
  283.    struct IntuiText labels[4] = {
  284.     {2,0,JAM2,5,15,NULL,
  285.         (UBYTE *)"scroll strings",NULL},
  286.     {2,0,JAM2,220,15,NULL,
  287.         (UBYTE *)"Prop fuzzy",&labels[0]},
  288.     {2,0,JAM2,470,15,NULL,
  289.         (UBYTE *)"image/highlight",&labels[1]},
  290.     {2,0,JAM2,200,108,NULL,
  291.         (UBYTE *)"free Vert and Horiz",&labels[2]}
  292. };
  293.  
  294.     
  295. /********************************/
  296. /* Declare NewWindow structure. */
  297. /********************************/
  298.  
  299. struct NewWindow NewControlWindow = {
  300.    20, 20,             /* start LeftEdge, TopEdge */
  301.    600, 160,           /* start Width, Height */
  302.    2, 3,               /* DetailPen, BlockPen */
  303.                /* IDCMP FLAGS */
  304.    GADGETUP | GADGETDOWN | CLOSEWINDOW | 
  305.         MOUSEMOVE | MENUPICK,    
  306.                /* Flags */
  307.    WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,   
  308.    &PropGadget_free,   /* Pointer to FirstGadget */
  309.    NULL,               /* no pointer to first CheckMark */
  310.    (UBYTE *)"Proportional Gadgets",/* Title */
  311.    NULL,               /* no Pointer to Screen */
  312.    NULL,               /* no Pointer to BitMap */
  313.    20, 20,             /* Min size (no size allowed) */
  314.    600, 160,           /* Max size (no size allowed) */
  315.    WBENCHSCREEN        /* Type of screen */
  316. };
  317.  
  318. /****************************************************/
  319. /* Main program                                     */
  320. /****************************************************/
  321.  
  322. main()
  323. {
  324.    struct MenuItem *ItemAddress();
  325.    ULONG Signals, MIClass, MICode, itemnum;        
  326.    APTR MIAddress;
  327.    LONG gad_pos, real_pos;
  328.    SHORT MouseX, MouseY, first_line;
  329.    BOOL fix_strings;
  330.  
  331. /* Open libraries */
  332.  
  333.    if (!(IntuitionBase = (struct IntuitionBase *)
  334.     OpenLibrary("intuition.library", 
  335.         (LONG)LIBRARY_VERSION)))
  336.    {
  337.       printf("Can't open the intuition library\n");
  338.       MyCleanup();
  339.       exit(FALSE);
  340.    }
  341.  
  342.    if (!(GfxBase = (struct GfxBase *)
  343.     OpenLibrary("graphics.library", 
  344.         (LONG)LIBRARY_VERSION)))
  345.    {
  346.       printf("Can't open the graphics library\n");
  347.       MyCleanup();
  348.       exit(FALSE);
  349.    }
  350.  
  351. /*****************************************************/
  352. /* Set Prop Gadget variables for and motion          */
  353. /* of knob.                         */
  354. /*****************************************************/
  355.  
  356.    g_prop_scroll.Flags = AUTOKNOB | FREEVERT;
  357.    g_prop_scroll.VertBody = (ULONG)(visible_lines * 0xFFFF)
  358.                  / total_lines;
  359.  
  360.    g_prop_fuzzy.Flags = AUTOKNOB | FREEVERT;
  361.    g_prop_fuzzy.VertBody = 0x3000;
  362.  
  363.    g_prop_image.Flags = FREEVERT;
  364.    g_prop_image.VertBody = 0x3000;
  365.  
  366.    g_prop_free.Flags = FREEVERT | FREEHORIZ | PROPBORDERLESS;
  367.    g_prop_free.VertBody = 0x7FFF;
  368.    g_prop_free.HorizBody = 0x7FFF;
  369.  
  370. /*****************************************************/
  371. /* Call routine to assign strings to the IntuiText   */
  372. /* structures. We'll start at the top of the list.   */
  373. /*****************************************************/
  374.  
  375.    SetString(0);
  376.    PadString();
  377.  
  378. /*********************************************************/
  379. /* Open window in which to display Proportional Gadgets. */
  380. /*********************************************************/
  381.  
  382.       if (!(ControlWindow = 
  383.     (struct Window *)OpenWindow(&NewControlWindow)))
  384.       {
  385.          printf("Couldn't open the control window.\n");
  386.          MyCleanup();
  387.          exit(FALSE);
  388.       }
  389.  
  390.    PrintIText(ControlWindow->RPort, &labels[3], 0L, 0L);
  391.  
  392. /************************************************/
  393. /* Write out the strings to the window.        */
  394. /************************************************/
  395.    PrintIText(ControlWindow->RPort, &IntuiStr[4], 0L, 0L);
  396.  
  397. /*********************************************************/
  398. /* Loop forever until user clicks Close Gadget on window.*/
  399. /*********************************************************/
  400.    for (;;) {  /* wait for a signal and process it */
  401.  
  402.    fix_strings = FALSE;
  403.  
  404.       Signals = Wait(1L << 
  405.     ControlWindow->UserPort->mp_SigBit);
  406.  
  407.          /* Process the Intuition message. */
  408.          while (MyIntuiMessage=(struct IntuiMessage *)
  409.                       GetMsg(ControlWindow->UserPort)) 
  410.          {
  411.          /* Get all the needed info and reply to message */
  412.          MIClass = MyIntuiMessage->Class;
  413.          MICode = MyIntuiMessage->Code;
  414.          MIAddress = MyIntuiMessage->IAddress;
  415.      MouseX = MyIntuiMessage->MouseX;
  416.          MouseY = MyIntuiMessage->MouseY;
  417.  
  418.          ReplyMsg(MyIntuiMessage);
  419.  
  420.          /* Determine what the message was. */
  421.  
  422.            switch (MIClass) 
  423.        {
  424.          case GADGETUP:
  425.         first_line = ((ULONG)(total_lines - 
  426.           visible_lines) * g_prop_scroll.VertPot 
  427.           + (1L<<15)) >>16;
  428.  
  429.         SetString(first_line);
  430.         PadString();
  431.         PrintIText(ControlWindow->RPort, 
  432.             &IntuiStr[4], 0L, 0L);
  433.           break;
  434.  
  435.          case GADGETDOWN:
  436.          break;
  437.  
  438.          case MOUSEMOVE:
  439.  
  440. /****************************************************/
  441. /* We'll set a flag to tell ourselves to update the */
  442. /* strings. This way we don't update them for EVERY */
  443. /* mousemove message.                    */
  444. /****************************************************/
  445.         fix_strings = TRUE; 
  446.          break;
  447.  
  448. /* User clicked close Gadget. */
  449.              case CLOSEWINDOW:       
  450.  
  451. /* Reply to any outstanding messages. */
  452.              while (MyIntuiMessage =(struct IntuiMessage *)
  453.          GetMsg(ControlWindow->UserPort))
  454.                     ReplyMsg(MyIntuiMessage);
  455.              MyCleanup();
  456.              exit(TRUE);
  457.              break;
  458.  
  459.            }  /* switch */
  460.          } /* while */
  461.  
  462. /********************************************************/
  463. /* Check if we need to update list of strings.        */
  464. /* Call routine to assign strings to the IntuiText      */
  465. /* structures.                         */
  466. /********************************************************/
  467.     if (fix_strings)
  468.     {
  469.     first_line = ((ULONG)(total_lines - visible_lines) * 
  470.         g_prop_scroll.VertPot + (1L<<15)) >>16;
  471.     SetString(first_line);
  472.     PadString();
  473.     PrintIText(ControlWindow->RPort, 
  474.         &IntuiStr[4], 0L, 0L);
  475.     }
  476.  
  477.    }  /* for */
  478. }  /* main */
  479.  
  480. MyCleanup()
  481. {
  482.    if (ControlWindow) CloseWindow(ControlWindow);
  483.    if (GfxBase) CloseLibrary(GfxBase);
  484.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  485. }
  486.  
  487. /********************************************************/
  488. /* This routine assigns a string to each of the        */
  489. /* IntuiText structures we will display.         */
  490. /********************************************************/
  491. SetString(index)
  492. WORD index;
  493.  
  494. {
  495. int i;
  496.  
  497.    for(i=0; i<5; i++)
  498.     IntuiStr[i].IText = (UBYTE *)text_strings[index + i];
  499. }
  500.  
  501. /********************************************************/
  502. /* This routine will pad each string we have decided    */
  503. /* to display with blanks. In this example it is     */
  504. /* redundant - we will blank out the same strings over  */
  505. /* over. In a real application, however, we would most  */
  506. /* likely be changing the text in the text_strings array*/
  507. /* and would therefore need to re-blank them.        */
  508. /********************************************************/
  509. PadString()
  510. {
  511. int len,i,j;
  512.  
  513.    for (i=0; i<5; i++)
  514.    {
  515.       len = strlen(IntuiStr[i].IText);
  516.       for (j = len; j < MAXLEN - 1; j++)
  517.       {
  518.     * (IntuiStr[i].IText + j) = ' ';
  519.       }
  520.       * (IntuiStr[i].IText + MAXLEN - 1) = '\0';
  521.    }
  522. }
  523.